// 动态加载所有分类模块
(async function() {
  // 初始化categories对象
  window.categories = window.categories || {};
  
  // 设置默认值
  window.categories.defaults = { 
    "categoryId": "comment",
    "promptPrefix": "你是一个微博平台神评论专家，能根据输入，分析提起关键点，生成引人注目的神回复。"
  };

  try {
    // 获取所有分类模块文件
    const moduleFiles = [
      'categories/comment.js',
      'categories/role.js',
      'categories/chating.js',
      'categories/quotes.js',
      'categories/ascii.js',
      'categories/eq.js',
      'categories/writing.js',
      'categories/translate.js'
    ];

    // 动态加载所有模块
    const modules = await Promise.all(
      moduleFiles.map(file => {
        return new Promise((resolve) => {
          const script = document.createElement('script');
          script.src = file;
          script.onload = () => resolve();
          script.onerror = (e) => {
            console.error(`Failed to load module: ${file}`, e);
            resolve();
          };
          document.head.appendChild(script);
        });
      })
    );

    // 确保所有分类都已加载
    const loadedCategories = [
      window.categories.comment,
      window.categories.role,
      window.categories.chating,
      window.categories.quotes,
      window.categories.ascii,
      window.categories.eq,
      window.categories.writing,
      window.categories.translate
    ].filter(Boolean); // 过滤掉未加载的分类

    window.categories.categories = loadedCategories;
    
    // 触发分类加载完成事件
    const event = new CustomEvent('categories:loaded', { 
      detail: { categories: loadedCategories } 
    });
    document.dispatchEvent(event);
    
  } catch (error) {
    console.error('Error loading categories:', error);
  }
})();